<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta http-equiv="X-UA-Compatible" content="IE=edge">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>默认回调对象设计</title>
</head>
<body>
    <script>
        function Callback() {
            var list = [];
            var self;
            self = {
                add: function(fn) {
                    list.push(fn)
                },
                fire: function(data) {
                    list.forEach(function(task) {
                        task(data)
                    });
                }
            }
            return self
        }

        function test1(data) {
            console.log('test1~~~~',data)
        }
        function test2(data) {
            console.log('test2~~~~',data)
        }

        var cbs = Callback()
        cbs.add(test1)
        cbs.fire('hahaha')// test1~~~~ hahaha
        cbs.add(test2)
        cbs.fire('kakak')//test1~~~~ kakak  test2~~~~ kakak
    </script>
</body>
</html>